package show.me.the.money.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileFinder {
	public static File searchSingleFile(File root,String nm){
		File match = null;
		String matchName = nm.toLowerCase();
		if(root.isFile()){
			return null;
		}
		File[] filesOrDirs = root.listFiles();
		for(File f : filesOrDirs){
			if(f.isDirectory()){
				match = searchSingleFile(f,nm);
				if(match != null){
					break;
				}
			}
			else{
				if(f.getName().toLowerCase().equals(matchName)){
					match = f;
					break;
				}
			}
		}
		return match;
	}
	
	public static List<File> search(File root,String postfix){
		List<File> results = new ArrayList<File>();
		if(root.isFile()){
			return results;
		}
		File[] filesOrDirs = root.listFiles();
		for(File f : filesOrDirs){
			if(f.isDirectory()){
				results.addAll(search(f,postfix));
			}
			else{
				if(f.getName().toLowerCase().endsWith(postfix)){
					results.add(f);
				}
			}
		}
		return results;
	}
	
	public static List<File> search(String root,String postfix){
		return search(new File(root),postfix);
	}
	
	public static void main(String[] args){
		
		File root = new File("E:/Dev/workspace/SSDev/WebRoot/WEB-INF/config/dictionary");
		/*
		List<File> results = search(root,".xml");
		for(File f:results){
			System.out.println(f.getName());
		}
		*/
		File f = searchSingleFile(root,"hometown.xml");
		System.out.println(f);
	}
}
